package at.fhooe.mc.master.clc.client.widget;

import java.util.HashSet;
import java.util.Set;

import at.fhooe.mc.master.clc.client.widget.interfaces.IRadioButtonTable;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.RadioButton;

public class RadioButtonTable extends CheckBoxTable implements
		IRadioButtonTable
{
	private String radioGroupName;
	
	public RadioButtonTable(String radioGroupName)
	{
		this.radioGroupName = radioGroupName;
	}
	
	@Override
	public void addRow(String... fields)
	{
		int row = getRowCount();
		
		setWidget(row, 0, new RadioButton(this.radioGroupName));
		
		for (int i = 0; i < fields.length; i++)
		{
			setText(row, i + 1, fields[i]);
		}
	}
	
	@Override
	public void addClickedRow(boolean clicked, String... fields)
	{
		int row = getRowCount();
		
		RadioButton box = new RadioButton(this.radioGroupName);
		box.setValue(clicked);
		
		setWidget(row, 0, box);
		
		for (int i = 0; i < fields.length; i++)
		{
			setText(row, i + 1, fields[i]);
		}
	}
	
	// TODO: Test if the value selection actually works...
	@Override
	public Set<Integer> getSelectedRows()
	{
		Set<Integer> selectedRows = new HashSet<Integer>();
	    
	    for (int i = 0; i < getRowCount(); ++i) {
	      RadioButton radio = (RadioButton)getWidget(i, 0);
	      if (radio.getValue()) {
	        selectedRows.add(i);
	      }
	    }
	    
	    return selectedRows;
	}
	
}
